<?php
/*
==========================================
* 插件方法库
==========================================
* Copyright (c) 2012 伯仁网络 All rights reserved.
* Author: YinHailin
* $Id: GoodsFunc.class.php 10 2014-01-20 09:53:57Z YinHailin $
==========================================
*/

class GoodsFunc extends BasicMod {

	/*
	 * 获取商品分类
	 * @param ingeter $parent_id 分类父ID
	 * @param string $extend 扩展查询条件
	 * @param string $field 查询字段筛选
	 * @param string $order 排序方式
	 * @return array
	 */
	public function GetSortList($parent_id, $extend = 'status="1"', $field = '*', $order = 'sequence asc, id asc') {
		$objGoodsSort = M(self::$dataGoodsSort);
	
		$parent_id = intval($parent_id);
		$where = 'parent_id = '.$parent_id;
		$where = $extend === '' ? $where : $where.' AND '.$extend;
		$list = $objGoodsSort->Field($field)->Where($where)->Order($order)->Select();
		$list = deep_htmlspecialchars_decode($list);
		return $list;
	}

	/*
	 * 按分类获取商品列表
	 * @param ingeter $sort_id 分类ID
	 * @param string $limit 限制条件
	 * @param string $extend 扩展查询条件
	 * @param string $field 查询字段筛选
	 * @param string $order 排序方式
	 * @return array
	 */
	public function GetGoodsList($sort_id, $limit = 1, $extend = 'status="1"', $field = '*', $order = 'id desc') {
		$objGoods = M(self::$dataGoods);
	
		$where = 'sort_id in('.$sort_id.')';
		$where = $extend === '' ? $where : $where.' AND '.$extend;
		$list = $objGoods->Field($field)->Where($where)->Order($order)->Limit($limit)->Select();
		$list = deep_htmlspecialchars_decode($list);
		return $list;
	}

	/*
	 * 按分类获取商品列表_分页
	 * @param ingeter $sort_id 分类ID
	 * @param string $listRow 每页显示的数量
	 * @param string $extend 扩展查询条件
	 * @param string $field 查询字段筛选
	 * @param string $order 排序方式
	 * @return array
	 */
	public function GetGoodsListByPage($sort_id, $listRow = 10, $extend = 'd.status="1"', $field = 'd.*, s.sort_name', $order = 'd.id desc') {
		$objGoods = M(self::$dataGoods);
	
		$where = 'd.sort_id in('.$sort_id.')';
		$where = $extend === '' ? $where : $where.' AND '.$extend;
	
		$count = $objGoods->Alias('AS d')->Where($where)->Count();  //计算总数
		$p = new HyPage($count, $listRow);
		$list = $objGoods->Alias('AS d')->Field($field)->Join(get_real_table(self::$dataGoodsSort).' AS s ON d.sort_id = s.id')->Limit($p->FirstRow . ',' . $p->ListRow)->Where($where)->Order($order)->Select();
		$list = deep_htmlspecialchars_decode($list);
		$page = $p->Show();  //分页的导航条的输出变量
	
		return array('list'=>$list, 'page'=>$page);
	}

	/*
	 * 获取商品信息
	 * @param ingeter $sort_id 分类ID
	 * @param string $limit 限制条件
	 * @param string $extend 扩展查询条件
	 * @param string $field 查询字段筛选
	 * @param string $order 排序方式
	 * @return array
	 */
	public function GetGoods($id, $extend = 'status="1"', $field = '*') {
		$objGoods = M(self::$dataGoods);
	
		$id = intval($id);
		$where = $extend === '' ? '' : $extend.' AND ';
		$where .= 'id = '.$id;
		$data = $objGoods->Field($field)->Where($where)->Find();
		$data = deep_htmlspecialchars_decode($data);
		return $data;
	}
}